package CB_C19;

import javafx.scene.control.*;
import javax.swing.JFileChooser;
import javax.swing.JFileChooser.*;
import javafx.scene.media.*;
import javafx.stage.Alert.*;
import java.io.File;

import javafx.scene.CustomNode;
import javafx.scene.Group;
import javafx.scene.Node;
import java.net.URL;
import javax.media.*;
import javax.media.protocol.*;
import javax.media.format.*;

 /**Open Button*/
var open: Button;
 /**Save Button: Currently only saves an empty file*/
public var save: Button;
var displayURL: Label;
 /**OpenURL Button*/
var openURL: Button;
 /**chooseURL Button*/
var chooseURL: TextBox;
 /**close Button*/
var close: Button;
 /**file path for JFileChooser*/
var file_path: URL;
 /**URL for ChooseURL Button*/
var url_text: URL;
var save_path: URL;
 /**JFileChooser*/
var filechooser: JFileChooser = new JFileChooser();
 /**file for JFileChooser*/
var file_stream : File;
 /**media for JFileChooser*/
var new_media : Media = null;
 /**JFileChooser for saving*/
var filesaver: JFileChooser = new JFileChooser();
 /**boolean skip, true if fail to open file*/
var skip :Boolean = false;
 /**boolean show*/
var show : Boolean = false;
/** Processor */
public var processor: MyProcessor = null;

var sink : DataSink;
var output : DataSource;
var ds : DataSource;
var clonedDataSource : DataSource;
var dest : MediaLocator;
var formats : Format[];
var proc : Processor;

/**Open_Save
 * Created on 10.11.2009. <br>
 <br>
 * Represents the Open, Save URL and Close Buttons
 *<br>
 * @author Sebastian Czekierski, Stefan Blumberger
 *
 *
 */
 
public class gui_Open_Save extends CustomNode
{
    override function create(): Node
    {
        return Group
        {
            content:
            [
                open = Button
                {
                    translateX: 0;
                    text: "Open";
                    action: function()
                    {
                        skip = false;
                        filechooser.setDialogTitle("Open Media-File");
                        filechooser.setDragEnabled(true);
                        if(JFileChooser.APPROVE_OPTION == filechooser.showOpenDialog(null))
                        {
                           file_stream = filechooser.getSelectedFile();
                           file_path = file_stream.toURL();
                           create_new_media(file_path);
                        }
                        else
                        {
                            inform("Fail to open File");
                            skip = true
                        }
                    }
                }
                openURL = Button
                {
                    translateX: open.width +open.translateX + 10;;
                    text: "URL";
                    action: function()
                    {
                        if(show)
                        {
                            show = false;
                        }
                        else
                        {
                            show = true;
                        }
                    }
                }
                save = Button
                {
                    translateX: openURL.width + openURL.translateX + 10;
                    text: "Save";
                    disable:true;
                    action: function()
                    {
                        Main.class_play_timeline.play.disable = true;
                        filechooser.setDialogTitle("Save Media-File");
                        if(JFileChooser.APPROVE_OPTION == filechooser.showSaveDialog(null))
                        {
                           var filetmp = filechooser.getSelectedFile();
                           save_path = filechooser.getSelectedFile().toURL();
                           if(filetmp.exists())
                           {
                               inform("File bereits vorhanden");
                           }
                           else
                           {
                                save_file();
                           }
                        }
                    }
                }
                close = Button
                {
                    translateX: save.width +save.translateX + 10;
                    text: "Close";
                    action: function()
                    {
                       processor.playerstop();
                       Main.our_stage.close();
                    }
                }
                displayURL = Label
                {
                        translateX: close.width + close.translateX + 10;
                        translateY: 5;
                        text: ("URL:")
                        visible: bind show;
                }
                chooseURL = TextBox
                {
                      translateX: close.width + close.translateX + 50;
                      width: 300;
                      visible: bind show;
                      action: function()
                      {
                            url_text = new URL(chooseURL.text);
                            skip = false;
                            if(url_text == null)
                            {
                                skip = true;
                            }
                            create_new_media(url_text);
                      }
                }
            ]
        }
    }
    function create_new_media(new_source :URL)
    {
        if(not(skip))
        {
            Main.p = new MyProcessor(
                new_source.toExternalForm(),
                Main.class_effect.enable_effect.selected,
                Main.class_brightness.brightness.value,
                Main.class_strength.frames.value,
                Main.class_strength.random.selected,
                Main.class_effect.duration.text,
                Main.class_effect.begin.text,
                Main.class_brightness.checkBox.selected
            );
            Main.class_play_timeline.play.disable = false;
            Main.class_open_save.save.disable = false;
            Main.class_play_timeline.play.text = "Play";
            Main.class_play_timeline.duration_var = Main.p.proc.getDuration();
            
            //Get_file_info + put_file_info
            Main.class_info.info_name.text = file_stream.getName();
            
            var video: VideoFormat = Main.p.in;
            Main.class_info.info_height.text = "Height: {video.getSize().height}";
            Main.class_info.info_width.text =  "Width: {video.getSize().width}";
        }
    }
    function save_file()
    {
        ds = Main.p.proc.getDataOutput();
        if(ds == null)
                inform("fail_null_pointer");
        var save : Save = new Save(ds, save_path,Main.p.proc);
        save.init();
    }
}

